找传奇、传世资源到传世资源站!

调用微信公众号消息推送

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

在微信公众号中对指定的已关注的用户进行消息推送
调用微信公众号消息推送 C#语言基础-第1张
public class WxArlam { public WxArlam() { token = GetBaseAccessToken(); wxyhids = GetUserOpenIdInfo(""); foreach (string s in wxyhids.data.openid) { wxyhs.Add(s, GetBaseWeChatUserInfo(s)); } } public string appid = ConfigurationManager.AppSettings["appid"].ToString(); public string secret = ConfigurationManager.AppSettings["secret"].ToString(); public string mbid = ConfigurationManager.AppSettings["mbid"].ToString(); public string wxjieshouren = ConfigurationManager.AppSettings["wxjieshouren"].ToString(); public DateTime lastDate = DateTime.Now; public DateTime SendTime = DateTime.Now; public BaseAccessTokenEntity token; WXUserOpenIdInfo wxyhids ;//关注公众号的微信用户ID集合 Dictionary<string, BaseWeChatUserEntity> wxyhs = new Dictionary<string, BaseWeChatUserEntity>();//<关注公众号的微信用户ID,关注公众号的微信用户详细信息> public void 公众号报警(string num) { try { //if (SendTime.AddMinutes(3) < DateTime.Now) //{ // SendTime = DateTime.Now; Console.WriteLine("推送公众号开始"); #region 推送内容(模版) var data = new DrawResultData { first = new MagTemplateValue { value = "你好,有新的报警", color = "#173177" }, keyword1 = new MagTemplateValue { value = "惟安", color = "#173177" }, keyword2 = new MagTemplateValue { value = "离线数量", color = "#173177" }, keyword3 = new MagTemplateValue { value = DateTime.Now.ToString(), color = "#173177" }, keyword4 = new MagTemplateValue { value = "惟安在线数量 " num, color = "#173177" }, remark = new MagTemplateValue { value = "请及时处理", color = "#173177" } }; #endregion string[] gzhs = wxjieshouren.Split(',');//设置的接收人 for (int i = 0; i < gzhs.Length; i ) { Console.WriteLine("接收人 " (i 1).ToString() " :" gzhs[i]); foreach (string s in wxyhids.data.openid) { if (wxyhs[s].nickname == gzhs[i])//用户昵称等于设置的用户 { Task task = new Task(() => { WeChatReturnCodeEntity a = SendTemplateMsg(s, mbid, "", data);//发推送 Console.WriteLine("发送结果 " a.errcode); //ceshilog("微信报警推送成功" gzhs[i]); if (a.errcode == 40001) { token = GetBaseAccessToken(); a = SendTemplateMsg(s, mbid, "", data);//发推送 } if (a.errcode == 42001) { token = GetBaseAccessToken(); a = SendTemplateMsg(s, mbid, "", data);//发推送 } Console.WriteLine(s "(" wxyhs[s].nickname ") errorcode:" a.errcode " errormsg:" a.errmsg); }); task.Start(); } } } Console.WriteLine("推送公众号结束"); //} } catch (Exception ex) { Console.WriteLine("推送公众号失败:" ex.Message); } finally { } } //基础支持:获取access_token。 public BaseAccessTokenEntity GetBaseAccessToken() { if (DateTime.Now >= lastDate)//token有效时间7200毫米,超过7200毫秒就需要重新获取,否则报错errorcode:40001 { var url = string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}" , appid, secret); var jsonContent = Encoding.UTF8.GetString(new WebClient().DownloadData(url)); if (string.IsNullOrEmpty(jsonContent)) return null; token =JsonConvert.DeserializeObject<BaseAccessTokenEntity>(jsonContent); lastDate = DateTime.Now.AddMilliseconds(token.expires_in); Console.WriteLine("获取Token :" token.access_token " " lastDate.ToString()); return token; } else { return token; } } //基础支持:获取用户OPENID。 public WXUserOpenIdInfo GetUserOpenIdInfo(string nextOpenId) { //var token = GetBaseAccessToken(); if (token == null || string.IsNullOrEmpty(token.access_token)) return null; var url = string.Format("https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}", token.access_token); if (!string.IsNullOrEmpty(nextOpenId)) { url = "&next_openid=" nextOpenId; } var jsonContent = Encoding.UTF8.GetString(new WebClient().DownloadData(url)); if (string.IsNullOrEmpty(jsonContent)) return null; var user = JsonConvert.DeserializeObject<WXUserOpenIdInfo>(jsonContent); return user; } //基础支持:获取用户信息。 public BaseWeChatUserEntity GetBaseWeChatUserInfo(string openid) { //var token = GetBaseAccessToken(); if (token == null || string.IsNullOrEmpty(token.access_token)) return null; var url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN" , token.access_token, openid); var jsonContent = Encoding.UTF8.GetString(new WebClient().DownloadData(url)); if (string.IsNullOrEmpty(jsonContent)) return null; var user = JsonConvert.DeserializeObject<BaseWeChatUserEntity>(jsonContent); return user; } //基础支持:获取用户信息。 public BaseWeChatUserEntity GetBaseWeChatUserInfo(string access_token, string openid) { var url = string.Format("https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}&lang=zh_CN" , access_token, openid); var jsonContent = Encoding.UTF8.GetString(new WebClient().DownloadData(url)); if (string.IsNullOrEmpty(jsonContent)) return null; var user = JsonConvert.DeserializeObject<BaseWeChatUserEntity>(jsonContent); return user; } //发送消息-模板消息接口 public WeChatReturnCodeEntity SendTemplateMsg(string openid, string templateid, string skipUrl, object jsonData) { // <param name="templateid">消息模板对应的唯一编号串</param> // <param name="skipUrl">模板点击跳转的链接</param> // <param name="jsonData">模板显示需要的json数据</param> //var token = GetBaseAccessToken(); if (token == null || string.IsNullOrEmpty(token.access_token)) return null; string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", token.access_token); var msg = new MagTemplateEntity<object> { touser = openid, template_id = templateid, url = skipUrl, topcolor = "#FF0000", data = jsonData }; var postJson = JsonConvert.SerializeObject(msg); byte[] sendData = Encoding.UTF8.GetBytes(postJson); string jsonContent = Encoding.UTF8.GetString(new WebClient().UploadData(url, "POST", sendData)); if (string.IsNullOrEmpty(jsonContent)) return null; var returnCode = JsonConvert.DeserializeObject<WeChatReturnCodeEntity>(jsonContent); return returnCode; } } /// <summary> /// OAuth2.0鉴权:通过code换取网页授权access_token,参数说明。 /// </summary> public class OAuthAccessTokenEntity { /// <summary> /// 网页授权接口调用凭证(【注意】此access_token与基础支持的access_token不同)。 /// </summary> public string access_token; public int expires_in; public string refresh_token; /// <summary> /// 用户唯一标识(【注意】在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID)。 /// </summary> public string openid; /// <summary> /// 用户授权的作用域,使用逗号(,)分隔。 /// </summary> public string scope; } /// <summary> /// 获取基础支持的access_token。 /// </summary> public class BaseAccessTokenEntity { /// <summary> /// 基础支持的access_token不同。 /// </summary> public string access_token; public int expires_in; } /// <summary> /// 创建二维码ticket。 /// </summary> public class QRcodeTicketEntity { public string ticket; public int expire_seconds; public string url; } public class BaseWeChatUserEntity { /// <summary> /// 是否订阅此公众号,0-未关注,将取不到其余信息。 /// </summary> public int subscribe; public string openid; public string nickname; public string headimgurl; public int sex; public string language; public string city; public string province; public string country; /// <summary> /// 关注时间。 /// </summary> public string subscribe_time; /// <summary> /// 绑定开发平台UnionID. /// </summary> public string unionid; /// <summary> /// 对粉丝的备注。 /// </summary> public string remark; /// <summary> /// 用户所在分组。 /// </summary> public int groupid; }

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复